/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package record;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author mgeden
 */
public class LocationPrefer {
    public LocationPrefer () 
    {
        //loadFromDataBase();
    } 
    private String location;
    private float latitude,longitude;
    private short altitude,temperature,pressure;
    private short timeZoneMin;
    private byte timeZoneSrc;
    private boolean isDSTOn, isItFirstTime=false;
  
    public String getLocation() {
        return location;
    }
    public float getLatitude() {
        return latitude;
    }

    public float getLongitude() {
        return longitude;
    }
    public double getTimeZone() {
        return timeZoneMin/60.0;
    }

    public byte getTimeZoneSrc() {
        return timeZoneSrc;
    }
     public boolean getIsDSTOn() {
        return isDSTOn;
    }
     public short getAltitude() {
        return altitude;
    }

    public short getTemperature() {
        return temperature;
    }

    public short getPressure() {
        return pressure;
    }
    public boolean getIsItFirstTime() {
        return isItFirstTime;
    }
    public void setLocationParameters(String location,float latitude,float longitude,int timeZoneSrc,float timeZone, boolean isDSTOn,short altitude,short temperature,short pressure )
    {   this.location=location;
        this.latitude=latitude;
        this.longitude=longitude;
        this.timeZoneSrc=(byte)timeZoneSrc;
        this.timeZoneMin=(short)(timeZone*60);
        this.isDSTOn=isDSTOn;
        this.altitude=altitude;
        this.temperature=temperature;
        this.pressure=pressure;
    }
    public void saveToDataBase ()
    {
        RecordStore recordStore;
        DataOutputStream outputStream;
        try {
            recordStore = RecordStore.openRecordStore("locationPrefer", true);
        } catch (RecordStoreException rsc) {
            return;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        outputStream = new DataOutputStream(baos);
        try {
            outputStream.writeUTF(location);
            outputStream.writeFloat(latitude);
            outputStream.writeFloat(longitude); 
            outputStream.writeByte(timeZoneSrc);
            outputStream.writeShort(timeZoneMin)  ;
            outputStream.writeBoolean(isDSTOn);
            outputStream.writeShort(altitude);
            outputStream.writeShort(temperature);
            outputStream.writeShort(pressure);
            byte[] bytes = baos.toByteArray();
            
            if (recordStore.getNumRecords() == 0)
                recordStore.addRecord(bytes, 0, bytes.length);
            else
                recordStore.setRecord(1, bytes, 0, bytes.length);
        } catch (Exception exception) {
        } finally {
            try {outputStream.close();} catch (Exception ex) {}
            try {recordStore.closeRecordStore();} catch (Exception ex) {}
        }
    }
    public void loadFromDataBase() {
        RecordStore recordStore = null;
        DataInputStream inputStream = null;
        try {
	    recordStore = RecordStore.openRecordStore("locationPrefer", false);
            byte[] bytes = recordStore.getRecord(1);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            inputStream = new DataInputStream(bais);
            location = inputStream.readUTF();
            latitude = inputStream.readFloat();
            longitude = inputStream.readFloat();       
            timeZoneSrc=inputStream.readByte();
            timeZoneMin = inputStream.readShort();
            isDSTOn=inputStream.readBoolean();
            altitude=inputStream.readShort();
            temperature=inputStream.readShort();
            pressure=inputStream.readShort();
                  
        } catch (Exception ex) {
            location ="İstanbul";
            latitude =41.02f;
            longitude = 28.97f;
            timeZoneSrc=1;//0;
            timeZoneMin =120;
            isDSTOn=false;
            altitude=0;
            temperature=10;
            pressure=1010;
            isItFirstTime=true;

            
        } finally {
            try {inputStream.close();} catch (Exception ex) {}
            try {recordStore.closeRecordStore();} catch (Exception ex) {}
        }
    }

}
